
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        
        document.addEventListener("backbutton", this.onBackKeyDown, false);
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    
    onBackKeyDown:function(){
    	Toast.showShort('再点击一次退出!'); 
        document.removeEventListener("backbutton", onBackKeyDown, false); // 注销返回键  
        document.addEventListener("backbutton", exitApp, false);//绑定退出事件  
        // 3秒后重新注册  
        var intervalID = window.setInterval(function() {  
            window.clearInterval(intervalID);  
            document.removeEventListener("backbutton", exitApp, false); // 注销返回键  
            document.addEventListener("backbutton", onBackKeyDown, false); // 返回键  
        }, 3000);  
        
    },
    
    exitApp:function(){
    	navigator.app.exitApp();  
    }
};
